/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tr.edu.ozyegin.ccrg.raid.monitor;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author becomen
 */
public class SystemMonitor {
    public static double getCPU() throws FileNotFoundException, IOException{
        FileInputStream st = new FileInputStream("/proc/stat");
        DataInputStream ds = new DataInputStream(st);
        String str1 = ds.readLine();
        ds.close();
        st.close();
        //long ts = System.nanoTime();
        try {
            Thread.sleep(750);
        } catch (InterruptedException ex) {
            Logger.getLogger(SystemMonitor.class.getName()).log(Level.SEVERE, null, ex);
        }
        //long tf = System.nanoTime();
        st = new FileInputStream("/proc/stat");
        ds = new DataInputStream(st);
        String str2 = ds.readLine();
        ds.close();
        st.close();
				
        String[] first = str1.split(" ");
        String[] second = str2.split(" ");
        try{
            int i1,i2,i3,i4,j1,j2,j3,j4;
            i1 = Integer.parseInt(first[2]);
            i2 = Integer.parseInt(first[3]);
            i3 = Integer.parseInt(first[4]);
            i4 = Integer.parseInt(first[5]);
            
            j1 = Integer.parseInt(second[2]);
            j2 = Integer.parseInt(second[3]);
            j3 = Integer.parseInt(second[4]);
            j4 = Integer.parseInt(second[5]);
            int t1,t2,t3,t4,t5,t6,t7;
            t1 = j1-i1;
            t2 = j2-i2;
            t3 = j3-i3;
            t4 = j4-i4;
            
            int total = t1+t2+t3+t4;
            double tu = ((double)t1)/((double)total);
            double tn = ((double)t2)/((double)total);
            double ts = ((double)t3)/((double)total);
            //long total2  = j1+j2+j3+j5+j6+j7;
             
            
            
            return tu+tn+ts;
            //return (double)total2/(double)(total2+j4);
            //System.out.println(((double)(tf-ts)/1000000000)+ " "+j1+" "+i1+ " "+ total);
            //return (double)total/(((double)(tf-ts)/1000000000)*(double)100);
        }
        catch(Exception ex){}
        return 0.0;
    }
    public static double getMemory() throws FileNotFoundException, IOException{
        FileInputStream st = new FileInputStream("/proc/meminfo");
        DataInputStream ds = new DataInputStream(st);
        //ArrayList<String> list = new ArrayList<String>();
        String line;
        long total = 0;
        long free = 0;
        long cached = 0;
        long buffers = 0;
        //long buffer = 0;
        while((line = ds.readLine())!=null){
            String[] tokens = line.split(" ");
            if(tokens[0].equals("MemTotal:")){
                total = Long.parseLong(tokens[tokens.length-2]);
            }
            else if(tokens[0].equals("MemFree:")){
                free = Long.parseLong(tokens[tokens.length-2]);
            }
            else if(tokens[0].equals("Cached:")){
                cached = Long.parseLong(tokens[tokens.length-2]);
            }
            else if(tokens[0].equals("Buffers:")){
                buffers = Long.parseLong(tokens[tokens.length-2]);
            }
          /*  else if(tokens[0].equals("Buffers:")){
                buffer = Long.parseLong(tokens[tokens.length-2]);
            }*/
        }
        ds.close();
        st.close();
        return ((double)(total-(free+cached+buffers)))/((double)total);
    }
}
